package android.team.app.network;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.team.app.R;
import android.team.app.info.WebserviceConfig;
import android.team.app.ui.BaseActivity;

public class AsyncHttpResponseProcess implements AsyncHttpResponseListener{
	private BaseActivity baseActivity; //de goi duoc cac phuong thuc ma viet tai lop nay
	private boolean isShowDialog = true;
	
	//xay dung phuong thuc khoi tao
	public AsyncHttpResponseProcess(BaseActivity activity){
		this.baseActivity = activity;
	}
	
	public AsyncHttpResponseProcess(BaseActivity activity, boolean isShow){
		this.baseActivity = activity;
		this.isShowDialog = isShow;
	}

	@Override
	public void before() {
		//truoc khi load du lieu se xu li tai day
		//neu load data hien thi dialog
		//neu dong bo khong hien thi dialog
		if(isShowDialog){
			//se show dialog
			baseActivity.showProgressDialog(baseActivity.getString(R.string.waiting));
		}
		
	}

	@Override
	public void after(int statusCode, HttpResponse response) {
		//phuong thuc duoc goi khi qua trinh load data ket thuc
		if(isShowDialog){
			//tai day se dong dialog
			baseActivity.hideProgressDialog();
		}
		
		// tai day se xu li ket qua tra ve dua vao statuscode (thanh cong thi xu li du lieu, khong thanh cong thi phai thong 
		// bao cho nguoi dung loi gi)
		switch (statusCode) {
		case AsyncHttpBase.STATUS_NETWORK_OFF:
			baseActivity.showPositiveCustomDialog(baseActivity, 
					R.string.error, 
					R.string.network_disconnect, 
					R.drawable.button_ok);
			//show dialog thong bao network ko co
			break;
		case AsyncHttpBase.STATUS_NETWORK_ERROR:
			//show dialog thong bao loi mang
			baseActivity.showPositiveCustomDialog(baseActivity, 
					R.string.error, 
					R.string.network_error, 
					R.drawable.button_ok);
			break;
		case AsyncHttpBase.STATUS_NETWORK_EXCEIPTION:
			//show dialog thong bao exceiption xay
			baseActivity.showPositiveCustomDialog(baseActivity, 
					R.string.error, 
					R.string.network_error, 
					R.drawable.button_ok);
			break;
		case AsyncHttpBase.STATUS_NETWORK_TIMEOUT:
			//show dialog thong bao xu li lau qua (thoi khong can)
			baseActivity.showPositiveCustomDialog(baseActivity, 
					R.string.error, 
					R.string.network_timeout, 
					R.drawable.button_ok);
			break;
		case AsyncHttpBase.STATUS_NETWORK_OK:
			//day la trang thai nhan response thanh cong
			// xu li xem response nhu the nao
			processHttpResponse(response);
			break;
			

		default:
			break;
		}
		
	}
	public void processHttpResponse(HttpResponse response){
		//tai day dua vao response se lay du lieu kieu string
		String jsonString = "";
		try {
			jsonString = EntityUtils.toString(response.getEntity());
			if(jsonString==null){
				//thong bao loi du lieu
				baseActivity.showPositiveCustomDialog(baseActivity, 
						R.string.error, 
						R.string.json_parser_error, 
						R.drawable.button_ok);
				return;
			}else{
				//bat dau xu li du lieu
				int success = 0; //that bai// 1 thanh cong
				JSONObject jsonObject = new JSONObject(jsonString);
				success = jsonObject.getInt(WebserviceConfig.PARAM_SUCCESS);
				if(success==1){
					//thanh cong
					processIfResponseSccuess(jsonObject);
				}else{
					//that bai
					processIfResponseFail();
				}
				
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	
	//xay dung phuong thuc de xu li du lieu thanh cong
	public void processIfResponseSccuess(JSONObject json){
		
	}
	
	//xay dung phuong thuc de xu li du lieu khi that bai
	public void processIfResponseFail(){
		
	}

}
